package cmd

import (
	"fmt"
	"os"
	"p3/player/mp3"
	"path"

	"github.com/spf13/cobra"
)

var mp3Cmd = &cobra.Command{
	Use:   "mp3",
	Short: "mp3 subcommand play mp3 music.",
	Run: func(cmd *cobra.Command, args []string) {
		mp3Play(cmd, args)
	},
}

func init() {
	rootCmd.AddCommand(mp3Cmd)
}

func mp3Play(cmd *cobra.Command, args []string) {
	if path.Ext(args[0]) != ".mp3" {
		fmt.Printf("Usage: %s mp3 file.mp3\n", os.Args[0])
		return
	}
	file, err := os.Open(args[0])
	if err != nil {
		panic("reading my-file.mp3 failed: " + err.Error())
	}
	mp3.Play(file)
}
